import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/common/charts_pie.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/investigation_plan_static_entity.dart';

class DangerInvestigationHomeController extends GetxController {
  final planData = <ChartData>[
    ChartData('已完成', 0, const Color(0xFF33A9FF)),
    ChartData('进行中', 0, const Color(0xFF5C6DFF)),
    //ChartData('漏检', 0, const Color(0xFFFD6B6B)),
  ];

  int get planDataCount {
    return planData.fold(0, (previousValue, element) => previousValue + element.y.toInt());
  }

  final missionData = <ChartData>[
    ChartData('已完成', 0, const Color(0xFF33A9FF)),
    ChartData('进行中', 0, const Color(0xFF5C6DFF)),
    //ChartData('漏检', 0, const Color(0xFFFD6B6B)),
  ];

  int get missionDataCount {
    return missionData.fold(0, (previousValue, element) => previousValue + element.y.toInt());
  }

  List<String> tableTitle = ['巡检点', '报警', '消警', '误报率'];

  Future<void> getInvestigationPlan() async {
    final resp = await HttpUtil()
        .generateGetRequest<InvestigationPlanStaticEntity>(
          Interface.getInvestigationPlan,
        )
        .execute();
    resp.onSuccess((result) {
      planData.clear();
      planData.addAll([
        ChartData('已完成', result.data?.completedSchedule.toDouble() ?? 0, const Color(0xFF33A9FF)),
        ChartData('进行中', result.data?.quantityToBeCompleted.toDouble() ?? 0, const Color(0xFF5C6DFF)),
        //ChartData('漏检', result.data?.missingInspectionPlan.toDouble() ?? 0, const Color(0xFFFD6B6B)),
      ]);
      update();
    });
  }

  Future<void> getInvestigationMission() async {
    final resp = await HttpUtil()
        .generateGetRequest<InvestigationPlanStaticEntity>(
          Interface.getInvestigationMission,
        )
        .execute();
    resp.onSuccess((result) {
      missionData.clear();
      missionData.addAll([
        ChartData('已完成', result.data?.completedSchedule.toDouble() ?? 0, const Color(0xFF33A9FF)),
        ChartData('进行中', result.data?.quantityToBeCompleted.toDouble() ?? 0, const Color(0xFF5C6DFF)),
        //ChartData('漏检', result.data?.missingInspectionPlan.toDouble() ?? 0, const Color(0xFFFD6B6B)),
      ]);
      update();
    });
  }

  @override
  void onReady() {
    super.onReady();
    getInvestigationPlan();
    getInvestigationMission();
  }
}
